<?php
/**
 * 多级审批流程
 */
class reverController extends baseController
{
	private function getCondition($type){
		$where = array();
		if($user_id=fetch_a_int('user_id')){
			$where[] = "t0.user_id={$user_id}";
		}
		if($audit=fetch_a_int('audit')){
			/*
			 * 未审状态为0，为方便在展现时key从1算，这里减回；
			 */
			$audit--;
			$where[] = "t1.pass={$audit}";
		}
		if($sno=fetch_a_string('sno')){
			/*
			 * 质检只能按单号进行
			 */
			$sno = Db::qstr($sno);
			if($type==Reviewer::CLS_QC){
				$where[] = "(t0.sno LIKE '{$sno}%' OR t0.indent_sno LIKE '{$sno}%')";
			}
			else{
				$where[] = "t0.sno LIKE '{$sno}%'";
			}
		}
		if($type=fetch_a_int('type')){
			$type -= 1;
			$where[] = "t0.type={$type}";
		}
		if($dept=fetch_a_int('dept')){
			$where[] = "t0.dept_id={$dept}";
		}
                if($r_pass=fetch_a_int('r_pass')){
			$where[] = "t0.r_pass={$r_pass}";
		}
                
		if($time=fetch_a_string('time')){
			$where[] = sprintf("(t0.addtime < %d and t0.addtime > %d)",
					strtotime("{$time} 00:00:00"), strtotime("{$time} 23:59:59") );
		}
                $from = fetch_a_string('from') ? fetch_a_string('from').' 00:00:00' : '';
		$to   = fetch_a_string('to') ? fetch_a_string('to').' 23:59:59' : '';
		if($from)  $where[] = ' `t0`.`addtime`>='.strtotime($from);
		if($to)	 $where[] = ' `t0`.`addtime`<='.strtotime($to);
                
		return join(' AND ', $where);
	}
	
	/**
	 * 需求审批
	 */
	public function demand(){
		$where = $this->getCondition(Reviewer::CLS_DEMAND);
		$count = ReviewerHelper::getDemandCountByUser(UID, $where);
		$limit = PagerHelper::split2limit($count);
		$list = ReviewerHelper::getDemandByUser( UID, $where, $limit[0], $limit[1] );
//		Debug::pre($list);
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * 订单审批
	 */
	public function indent(){
		$where = $this->getCondition(Reviewer::CLS_INDENT);
		$count = ReviewerHelper::getIndentCountByUser(UID, $where);
		$limit = PagerHelper::split2limit($count);
		$list = ReviewerHelper::getIndentByUser( UID, $where, $limit[0], $limit[1] );
//		Debug::pre($list);
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 领料审批
	 */
	public function outdemand(){
		$where = $this->getCondition(Reviewer::CLS_OUTDEMAND);
		$count = ReviewerHelper::getOutDemandCountByUser(UID, $where);
		$limit = PagerHelper::split2limit($count);
		$list = ReviewerHelper::getOutDemandByUser( UID, $where, $limit[0], $limit[1] );
//		Debug::pre($list);
		$this->assign('list', $list);
		$this->display();
	}
	
	/**
	 * 质检审批
	 */
	public function qc(){
		$where = $this->getCondition(Reviewer::CLS_QC);
		$count = ReviewerHelper::getQcCountByUser(UID, $where);
		$limit = PagerHelper::split2limit($count);
		$list = ReviewerHelper::getQcByUser( UID, $where, $limit[0], $limit[1] );
//		Debug::pre($list);
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * 进行审批
	 */
	public function flag(){
		$type = fetch_a_int('type');
		$id   = fetch_a_int('id');

		if(is_post()){
			$r = ReviewerHelper::audit(
					$type,
					$id,
					UID,
					fetch_a_int('pass') ? true : false,
					FilterHelper::sanitize(fetch_a_string('remark')));
			if($r){
				Show::note("保存审批结果成功");
			}
			else{
				Show::warn(ReviewerHelper::getLastError());
			}
		}

		if(!ReviewerHelper::validClass($type)){
			Show::warn("未知类型");
		}
		
		$this->assign('type', $type);
		$this->assign('id', $id);
		$this->display();
	}

	/**
	 * 保存审批结果
	 */
	public function save(){
		$r = ReviewerHelper::audit(
				fetch_a_int('type'),
				fetch_a_int('id'),
				UID,
				fetch_a_int('pass') ? true : false,
				FilterHelper::sanitize(fetch_a_string('remark')));
		if($r){
			Show::note("保存审批结果成功");
		}
		else{
			Show::warn(ReviewerHelper::getLastError());
		}
	}
	
}
?>